#!/usr/bin/env python

import os
import time
import random

entradas = 9
salidas = 2

# Python se traba para 10 entradas.
# C logro hacer 12 pero tarda 331 segundos.


def __int2bin(bits, num):
    '''Convert from an integer to a binary string.'''
    s = []
    opts = ['0', '1']
    for x in range(bits):
        s.insert(0,opts[num & 1])
        num >>= 1
    return ''.join(s)

def __create_table(N_entradas, N_salidas):
    random.seed(time.time())
    a = ['0','1']
    b = ['0','1', 'x']
    table = []
    for x in range(2**(N_entradas)):
        row = []
        row.extend( __int2bin(N_entradas,x) )
        map( row.extend, [ b[ random.randint(0,2) ] for y in range(N_salidas) ] )
        row.extend( '\n' )
        table.append( ''.join(row) )
    return ''.join(table)

def __ejecutar_test(cmd, table):
    cmd = "%s %s %s" % ( cmd, str(entradas), str(salidas) )
    print "# Command: ",cmd
    t = time.time()
    os.popen(cmd,'w').write(table)
    return time.time()-t

print
print "Language comparison test 1.0"
print "- This test doesnt do any floating point test."
print "- This test does intensive use of strings, lists, iterative structures and conditional structures."
print
print "- Generating evaluating table with "+str(2**entradas)+" possible input values."
table = __create_table( entradas , salidas)





print "- Implementacion en C:"
os.chdir(os.getcwd()+"/C")
os.system(os.getcwd()+"/compilar.run")
t_c_bien = __ejecutar_test(os.getcwd()+"/main", table)
print str(t_c_bien)+" segundos"
print


print "- Implementacion en python:"
os.chdir(os.getcwd()+"/../Python")
os.system(os.getcwd()+"/compilar.run")
t_python = __ejecutar_test(os.getcwd()+"/main.py", table)
print str(t_python)+" segundos"
print




print

print "Resultados:"
print "Python es ",str(t_python/t_c_bien), " veces mas lento que C."




